<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
// import { useI18n } from '@/hooks/web/useI18n'
// import { ElButton, ElMessageBox } from 'element-plus'
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTableTest'
import { reactive, PropType } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
// import { useIcon } from '@/hooks/web/useIcon'
// 获取字典
import { useCache } from '@/hooks/web/useCache'
import {
  //   spForageInOutOrderDelApi,
  //   spForageInOutOrderEndApi,
  spForageStockInOutLogListApi
} from '@/api/sp/food/index'
// import { spForageInOutOrderAddAdminApi, spForageInOutOrderListApi } from '@/api/cy/FoodGoTo'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  // console.log(type)
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  },
  titleData: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  }
})
console.log(props.currentRow.id, 'props')
console.log(props.titleData, 'propstitleData')

// const iconSee = useIcon({ icon: 'ep:view' })
const spForageStockInOutLogList = (val) => {
  if (props.currentRow.id) {
    val.forage_specs_id = props.currentRow.id
  } else {
    val.forage_id = props.titleData.id
  }
  return spForageStockInOutLogListApi(val)
}
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: spForageStockInOutLogList,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList, setSearchParams } = methods

getList()

// const { t } = useI18n()
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '主键',
    fixed: 'left',
    width: 60,
    table: {
      show: true
    },
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'forage_name',
    label: '饲料名称',
    width: 200,
    search: {
      show: true
    }
  },
  {
    field: 'forage_specs_name',
    label: '饲料规格',
    width: 200,
    search: {
      show: true
    }
  },
  {
    field: 'code',
    label: '饲料出入库编号',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'in_out_type',
    label: '类型',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('in_out_type', cellValue)
    },
    table: {
      show: true
    }
  },
  {
    field: 'out_type',
    label: '出库类型',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('out_type', cellValue)
    },
    table: {
      show: true
    }
  },
  {
    field: 'in_type',
    label: '入库类型',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('in_type', cellValue)
    },
    table: {
      show: true
    }
  },
  {
    field: 'number',
    label: '数量',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 200,
    form: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        'is-range': true,
        'unlink-panels': true,
        rangeSeparator: 'To',
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  }
  //   {
  //     field: 'action',
  //     width: 250,
  //     fixed: 'right',
  //     label: t('tableDemo.action'),
  //     form: {
  //       show: false
  //     },
  //     detail: {
  //       show: false
  //     }
  //   }
])

// 删除
// const apiDel = async () => {
//   let delRes = await spForageInOutOrderDelApi({ id })
//     .catch(() => {})
//     .finally(() => {})
//   if (delRes) {
//     getList()
//   }
// }

// 归档
// const apicheck = async () => {
//   let delRes = await spForageInOutOrderEndApi({ id })
//     .catch(() => {})
//     .finally(() => {})
//   if (delRes) {
//     getList()
//   }
// }
const { allSchemas } = useCrudSchemas(crudSchemas)

// const dialogVisible = ref(false)

// const dialogTitle = ref('')

// const actionType = ref('')

// const action = async (type: string, row: any = {}) => {
//   actionType.value = type
//   switch (type) {
//     case 'see':
//       dialogTitle.value = '查看'
//       tableObject.currentRow = row
//       dialogVisible.value = true
//       break
//     case 'del':
//       ElMessageBox.confirm('确定要删除这个出入库单吗？', '删除', {
//         confirmButtonText: '确认',
//         cancelButtonText: '取消',
//         type: 'warning'
//       })
//         .then(() => {
//           console.log(row.id)
//           //   apiDel(Number(row.id))
//         })
//         .catch(() => {
//           console.log('这里是取消confirm弹框')
//         })
//       break
//     case 'shen':
//       ElMessageBox.confirm('确定要归档吗？', '审核', {
//         confirmButtonText: '确认',
//         cancelButtonText: '取消',
//         type: 'warning'
//       })
//         .then(() => {
//           console.log(row.id)
//           //   apicheck(Number(row.id))
//         })
//         .catch(() => {
//           console.log('这里是取消confirm弹框')
//         })
//       break
//     default:
//       console.log('未知类型')
//   }
// }
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
    >
      <!-- <template #action="{ row }">
        <ElButton circle :icon="iconSee" @click="action('see', row)" title="查看" />
      </template> -->
    </Table>
  </ContentWrap>
</template>
